const {createAddress,getAddrListOpera,updateAddress,removeAddress}=require('../service/addr.server')
class AddrController{
    async createAddr(ctx){
        const user_id=ctx.state.user.id
        const {selected,telephone,address,buyPeople}=ctx.request.body
        const res=await createAddress({
            user_id,buyPeople,selected,address,telephone
        })
        ctx.body={
            message:"添加地址成功",
            res:res
        }}
    async getAddrList(ctx){
        const user_id=ctx.state.user.id
        try {
            const res=await getAddrListOpera(user_id)
            ctx.body={
                message: "获取列表成功",
                result:res
            }
        }catch (err){
            ctx.body={
                message:"获取列表失败",
                result: err
            }
        }

    }
    async updateAddr(ctx){
        const user_id=ctx.state.user.id
        const id=ctx.request.params.id
        try {
            const res=await updateAddress(user_id,id,ctx.request.body)
            ctx.body={
                message:"修改地址成功",
                result:res
            }
        }catch (err){
            ctx.body={
                message:"修改地址失败"
            }
        }

    }
    async remove(ctx){
        const user_id=ctx.state.user.id
        const id=ctx.request.params.id
       try {
           const res=await removeAddress(user_id,id)
           ctx.body={
               message:"删除地址成功",
               result:res
           }
       }catch (err){
            ctx.body={
                message:"删除地址错误"
            }
       }

    }

}
module.exports=new AddrController()